package com.wu.util;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Cousin
 * @date 2025/1/3
 * @Description
 */
public class LyricsDurationExtractorUtil {

    public static int extractTotalDuration(String lyric) {
        if (StringUtils.isEmpty(lyric)) {
            return 0;
        }
        String[] lyrics = lyric.split("\\n");
        if(CollectionUtil.isEmpty(Arrays.asList(lyrics))){
            return 0;
        }
        lyric = lyrics[lyrics.length-1];
        int totalSeconds = 0;
        String regex = "\\[(\\d{2}):(\\d{2})\\.(\\d{3})]";
        Pattern pattern = Pattern.compile(regex);


            Matcher matcher = pattern.matcher(lyric);
            if (matcher.find()){
                int minutes = Integer.parseInt(matcher.group(1));
                int seconds = Integer.parseInt(matcher.group(2));
                totalSeconds += minutes * 60 + seconds;
            }

        return totalSeconds;
    }

    public static void main(String[] args) {

    String lyric ="[00:00.00] 作曲 : 刘吉宁\n" +
            "[00:01.00] 作词 : 周毅\n" +
            "[00:26.800]烽烟起寻爱似浪淘沙\n" +
            "[00:33.700]遇见她如春水映梨花\n" +
            "[00:40.400]挥剑断天涯相思轻放下\n" +
            "[00:46.900]梦中我痴痴牵挂\n" +
            "[00:53.900]顾不顾将相王侯\n" +
            "[00:55.800]管不管万世千秋\n" +
            "[00:57.800]求只求爱化解\n" +
            "[00:58.900]这万丈红尘纷乱永无休\n" +
            "[01:01.400]爱更爱天长地久\n" +
            "[01:02.800]要更要似水温柔\n" +
            "[01:04.800]谁在乎谁主春秋\n" +
            "[01:06.900]一生有爱何惧风飞沙\n" +
            "[01:10.100]悲白发留不住芳华\n" +
            "[01:13.900]抛去江山如画换她笑面如花\n" +
            "[01:17.100]抵过这一生空牵挂\n" +
            "[01:20.100]心若无怨爱恨也随他\n" +
            "[01:23.300]天地大情路永无涯\n" +
            "[01:27.100]只为她袖手天下\n" +
            "[01:32.900]\n" +
            "[02:00.900]顾不顾将相王侯\n" +
            "[02:02.700]管不管万世千秋\n" +
            "[02:03.900]求只求爱化解\n" +
            "[02:05.800]这万丈红尘纷乱永无休\n" +
            "[02:07.900]爱更爱天长地久\n" +
            "[02:09.900]要更要似水温柔\n" +
            "[02:11.800]谁在乎谁主春秋\n" +
            "[02:13.400]一生有爱何惧风飞沙\n" +
            "[02:16.700]悲白发留不住芳华\n" +
            "[02:20.900]抛去江山如画换她笑面如花\n" +
            "[02:24.600]抵过这一生空牵挂\n" +
            "[02:26.900]心若无怨爱恨也随他\n" +
            "[02:30.700]天地大情路永无涯\n" +
            "[02:33.900]只为她袖手天下\n" +
            "[02:39.900]\n" +
            "[02:40.300]一生有爱何惧风飞沙\n" +
            "[02:43.700]悲白发留不住芳华\n" +
            "[02:47.400]抛去江山如画换她笑面如花\n" +
            "[02:50.900]抵过这一生空牵挂\n" +
            "[02:53.900]心若无怨爱恨也随他\n" +
            "[02:56.900]天地大情路永无涯\n" +
            "[02:59.900]只为她袖手天下\n" +
            "[03:05.900]\n" +
            "[03:06.900]烽烟起寻爱似浪淘沙\n" +
            "[03:13.300]遇见她如春水映梨花\n" +
            "[03:20.100]挥剑断天涯相思轻放下\n" +
            "[03:26.900]梦中我痴痴牵挂\n" +
            "[03:32.900]";
        try {
            int durationInSeconds = extractTotalDuration(lyric);
            System.out.println("Total duration in seconds: " + durationInSeconds);
        } catch (IllegalArgumentException e) {
            System.err.println("Error: " + e.getMessage());
        }
    }
}
